/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AlternativasyCriterios.java
 *
 * Created on 28/12/2011, 17:19:48
 */

package GUI;

import java.util.ArrayList;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

/**
 *
 * @author Tato
 */
public class AlternativasyCriterios extends javax.swing.JFrame {

    /** Creates new form AlternativasyCriterios */
    public AlternativasyCriterios(int cantAlt,int cantCri, Principal principal) {
        initComponents();
        cantidadAlternativas = cantAlt;
        cantidadCriterios = cantCri;
        nombreCriterios = new ArrayList<String>();
        nombreAlternativas = new ArrayList<String>();
        this.principal = principal;
        setearNombres();
        cargarComboAlternativas(0);
        cargarComboCriterios(0);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanelAlternativasyCriterios = new javax.swing.JPanel();
        comboAlternativas = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        txtNombreAlternativa = new javax.swing.JTextField();
        btnCambiarNombreAlternativa = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        comboCriterios = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        txtNombreCriterio = new javax.swing.JTextField();
        btnCambiarNombreCriterio = new javax.swing.JButton();
        btnAceptar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Aternativas y Criterios");

        jPanelAlternativasyCriterios.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createTitledBorder(""), "Alternativas"));
        jPanelAlternativasyCriterios.setToolTipText("");

        comboAlternativas.setToolTipText("");
        comboAlternativas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboAlternativasActionPerformed(evt);
            }
        });

        jLabel1.setText("Nombre :");

        btnCambiarNombreAlternativa.setText("Cambiar");
        btnCambiarNombreAlternativa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCambiarNombreAlternativaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanelAlternativasyCriteriosLayout = new javax.swing.GroupLayout(jPanelAlternativasyCriterios);
        jPanelAlternativasyCriterios.setLayout(jPanelAlternativasyCriteriosLayout);
        jPanelAlternativasyCriteriosLayout.setHorizontalGroup(
            jPanelAlternativasyCriteriosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelAlternativasyCriteriosLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelAlternativasyCriteriosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelAlternativasyCriteriosLayout.createSequentialGroup()
                        .addGroup(jPanelAlternativasyCriteriosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(comboAlternativas, javax.swing.GroupLayout.PREFERRED_SIZE, 212, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanelAlternativasyCriteriosLayout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(txtNombreAlternativa, javax.swing.GroupLayout.DEFAULT_SIZE, 158, Short.MAX_VALUE)))
                        .addContainerGap(24, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelAlternativasyCriteriosLayout.createSequentialGroup()
                        .addComponent(btnCambiarNombreAlternativa)
                        .addGap(72, 72, 72))))
        );
        jPanelAlternativasyCriteriosLayout.setVerticalGroup(
            jPanelAlternativasyCriteriosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelAlternativasyCriteriosLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(comboAlternativas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(27, 27, 27)
                .addGroup(jPanelAlternativasyCriteriosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtNombreAlternativa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(btnCambiarNombreAlternativa)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createTitledBorder("Criterios")));
        jPanel1.setToolTipText("");

        comboCriterios.setToolTipText("");
        comboCriterios.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboCriteriosActionPerformed(evt);
            }
        });

        jLabel2.setText("Nombre :");

        btnCambiarNombreCriterio.setText("Cambiar");
        btnCambiarNombreCriterio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCambiarNombreCriterioActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(comboCriterios, javax.swing.GroupLayout.PREFERRED_SIZE, 212, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(txtNombreCriterio, javax.swing.GroupLayout.DEFAULT_SIZE, 158, Short.MAX_VALUE)))
                        .addContainerGap(23, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(btnCambiarNombreCriterio)
                        .addGap(71, 71, 71))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(comboCriterios, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(27, 27, 27)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtNombreCriterio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(btnCambiarNombreCriterio)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        btnAceptar.setText("Aceptar");
        btnAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAceptarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(btnAceptar, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanelAlternativasyCriterios, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanelAlternativasyCriterios, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(btnAceptar, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAceptarActionPerformed

        principal.nombreAlternativas = nombreAlternativas;
        principal.nombreCriterios = nombreCriterios;
//        Principal principal = new Principal();
//        principal.nombreAlternativas = this.getNombreAlternativas();
//        principal.nombreCriterios = this.getNombreCriterios();
        this.dispose();
        //llamar a la principal
    }//GEN-LAST:event_btnAceptarActionPerformed

    private void btnCambiarNombreAlternativaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCambiarNombreAlternativaActionPerformed
        cambiarNombreAlternativa(txtNombreAlternativa,comboAlternativas);
    }//GEN-LAST:event_btnCambiarNombreAlternativaActionPerformed

    private void btnCambiarNombreCriterioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCambiarNombreCriterioActionPerformed
        cambiarNombreCriterio(txtNombreCriterio, comboCriterios);
    }//GEN-LAST:event_btnCambiarNombreCriterioActionPerformed

    private void comboAlternativasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboAlternativasActionPerformed
        mostrarNombreAlternativa(txtNombreAlternativa, comboAlternativas);
    }//GEN-LAST:event_comboAlternativasActionPerformed

    private void comboCriteriosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboCriteriosActionPerformed
        mostrarNombreCriterio(txtNombreCriterio, comboCriterios);
    }//GEN-LAST:event_comboCriteriosActionPerformed

    /**
    * @param args the command line arguments
    */
    /*public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new AlternativasyCriterios().setVisible(true);
            }
        });
    }*/

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAceptar;
    private javax.swing.JButton btnCambiarNombreAlternativa;
    private javax.swing.JButton btnCambiarNombreCriterio;
    private javax.swing.JComboBox comboAlternativas;
    private javax.swing.JComboBox comboCriterios;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanelAlternativasyCriterios;
    private javax.swing.JTextField txtNombreAlternativa;
    private javax.swing.JTextField txtNombreCriterio;
    // End of variables declaration//GEN-END:variables
    private ArrayList<String> nombreCriterios;
    private ArrayList<String> nombreAlternativas;
    private int cantidadCriterios;
    private int cantidadAlternativas;
    private Principal principal;


    private void cargarComboAlternativas(int indice){

        if(comboAlternativas.getItemCount()>0)
        {
            comboAlternativas.removeAllItems();
        }

        for (int i = 0; i < nombreAlternativas.size(); i++)
        {
            comboAlternativas.addItem(nombreAlternativas.get(i));
        }

        comboAlternativas.setSelectedIndex(indice);
        txtNombreAlternativa.setText(nombreAlternativas.get(indice));
    }

     private void cargarComboCriterios(int indice){

        if(comboCriterios.getItemCount()>0)
        {
            comboCriterios.removeAllItems();
        }

        for (int i = 0; i < nombreCriterios.size(); i++)
        {
            comboCriterios.addItem(nombreCriterios.get(i));
        }
        comboCriterios.setSelectedIndex(indice);
        txtNombreCriterio.setText(nombreCriterios.get(indice));
    }

    private void cambiarNombreAlternativa(JTextField jtf, JComboBox jcb)
    {
        if(jtf.getText().compareTo("")!=0){
            nombreCriterios.set(jcb.getSelectedIndex(), jtf.getText());
        }
        else
        {
            JOptionPane.showMessageDialog(this,
        "El nombre no puede estra vacio", "Mal formato",JOptionPane.WARNING_MESSAGE);
                    return;
        }

        cargarComboAlternativas(jcb.getSelectedIndex());

    }

    private void cambiarNombreCriterio(JTextField jtf, JComboBox jcb)
    {
        if(jtf.getText().compareTo("")!=0){
            nombreCriterios.set(jcb.getSelectedIndex(), jtf.getText());
        }
        else
        {
            JOptionPane.showMessageDialog(this,
        "El nombre no puede estra vacio", "Mal formato",JOptionPane.WARNING_MESSAGE);
                    return;
        }

        cargarComboCriterios(jcb.getSelectedIndex());
    }

    private void setearNombres()
    {
        for (int i = 0; i < cantidadAlternativas; i++)
        {
            nombreAlternativas.add("Alternativa"+(i+1));
        }

        for (int i = 0; i < cantidadCriterios; i++)
        {
            nombreCriterios.add("Criterio"+(i+1));
        }
    }

     private void mostrarNombreAlternativa(JTextField jtf, JComboBox jcb)
    {
         if(jcb.getItemCount()>0)
         {
          jtf.setText(nombreAlternativas.get(jcb.getSelectedIndex()));// cuando cambio un nombre se llama y trae index -1
        }
        
    }

      private void mostrarNombreCriterio(JTextField jtf, JComboBox jcb)
    {
          if(jcb.getItemCount()>0)
         {
             jtf.setText(nombreCriterios.get(jcb.getSelectedIndex()));
         }
    }

    
}
